package com.rran.study.chat.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.rran.study.chat.common.model.Response;
import com.rran.study.chat.common.utils.BeanCopyUtil;
import com.rran.study.chat.common.utils.UUIDUtil;
import com.rran.study.chat.user.dto.request.LoginWithPwdDTO;
import com.rran.study.chat.user.dto.request.LoginWithSmsDTO;
import com.rran.study.chat.user.dto.request.QueryUserInfoDTO;
import com.rran.study.chat.user.dto.request.UserInfoDTO;
import com.rran.study.chat.user.dto.response.LoginVO;
import com.rran.study.chat.user.dto.response.UserInfoVO;
import com.rran.study.chat.user.entity.AccountDO;
import com.rran.study.chat.user.entity.UserInfoDO;
import com.rran.study.chat.user.enums.AccountErrorEnum;
import com.rran.study.chat.common.exception.BusinessException;
import com.rran.study.chat.user.mapper.AccountMapper;
import com.rran.study.chat.user.mapper.UserMapper;
import com.rran.study.chat.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author yy
 * @Type UserServiceImpl.java
 * @Desc
 * @date 2020/8/11 11:49
 */
@Service
@Slf4j
@RefreshScope
public class UserServiceImpl implements UserService {


    @Value("${user.kafka.topic.syncUserInfo}")
    private String topic;

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    @Transactional
    public Response<LoginVO> loginWithSms(LoginWithSmsDTO loginWithSmsDTO) {
        AccountDO account = accountMapper.selectOneByAccount(loginWithSmsDTO.getAccount());
        String beanId = "";
        //如果是新用户
        if (account == null) {
            beanId = UUIDUtil.getNumber(16);
            AccountDO accountDO = new AccountDO();
            accountDO.setAccount(loginWithSmsDTO.getAccount());
            accountDO.setPassword("123456");
            accountDO.setBeanId(beanId);
            //注册用户
            accountMapper.insert(accountDO);

            UserInfoDO userInfoDO = new UserInfoDO();
            userInfoDO.setPhone(accountDO.getAccount());
            userInfoDO.setBeanId(accountDO.getBeanId());
            userInfoDO.setNick(UUIDUtil.getCharAndNumr(6));
            userMapper.insert(userInfoDO);

            try {
                String content = JSON.toJSONString(userInfoDO);
                log.info("发送kafka消息===>向好友中心同步用户基本信息,入参:content:{}", content);
                kafkaTemplate.send(topic, content);
            } catch (Exception e) {
                log.error("发送kafka消息===>向好友中心同步用户基本信息失败，异常信息:{},beanId:{}", e.getMessage(), beanId);
            }

        } else {
            beanId = account.getBeanId();
        }
        LoginVO result = new LoginVO();
        result.setBeanId(beanId);
        //todo get token
        result.setAccessToken("fqfsafas12123412312eawqsedawqdesa");
        result.setRefashToken("sfdafafjiji1312312312jhesakj12121");
        return new Response<>(result);
    }

    @Override
    public Response<LoginVO> loginWithPwd(LoginWithPwdDTO loginWithPwdDTO) {
        AccountDO account = accountMapper.selectOneByAccount(loginWithPwdDTO.getAccount());
        if (account == null) {
            throw new BusinessException(AccountErrorEnum.ACCOUNT_IS_NOT_FOUND);
        }

        //todo 正规的密码校验
        if (!loginWithPwdDTO.getPassword().equals(account.getPassword())) {
            throw new BusinessException(AccountErrorEnum.ACCOUNT_PASSWORD_MISMATCH);
        }

        //todo get token
        String accessToken = "fqfsafas12123412312eawqsedawqdesa";
        String refashToken = "sfdafafjiji1312312312jhesakj12121";
        //todo set to redis
        LoginVO result = new LoginVO();
        result.setBeanId(account.getBeanId());
        result.setAccessToken(accessToken);
        result.setRefashToken(refashToken);
        return new Response<>(result);
    }

    @Override
    public Response<UserInfoVO> getUserInfo(QueryUserInfoDTO queryUserInfoDTO) {
        UserInfoDO userInfoDO = new UserInfoDO();
        BeanCopyUtil.copyFromSrcToDest(queryUserInfoDTO, userInfoDO);
        UserInfoDO result = userMapper.queryUserInfo(userInfoDO);
        if (result == null) {
            return Response.success(new UserInfoVO());
        }
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanCopyUtil.copyFromSrcToDest(result, userInfoVO);
        return new Response<>(userInfoVO);
    }

    @Override
    public Response updateUserInfo(UserInfoDTO userInfoDTO) {
        //todo
        return null;
    }

}
